package com.lovo.psc.controller;

import com.alibaba.fastjson.JSON;
import com.lovo.commom.dto.OrderPiCiDto;
import com.lovo.psc.bean.OrderInfoBean;
import com.lovo.psc.bean.OrderProductBean;
import com.lovo.psc.bean.SupplyInfoBean;
import com.lovo.psc.dto.OrderDto;
import com.lovo.psc.service.IOrderInfoService;
import com.lovo.psc.service.IOrderProductService;
import com.lovo.psc.service.ISupplyInfoService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("info")
public class IOrderInfoController {
    @Autowired
    private IOrderInfoService service;
    private OrderInfoBean infoBean = new OrderInfoBean();

    @Autowired
    private ISupplyInfoService iSupplyInfoService;
    @Autowired
    private IOrderProductService productService;
    @Autowired
    RabbitTemplate rabbitTemplate;


    @GetMapping("add")
    public void add(String orderId, float totalMoney, int status) {
        infoBean.setOrderIdNum(orderId);
        infoBean.setOrderPrice(totalMoney);
        infoBean.setStatus(status);
    }

    @GetMapping("adds")
    private void adds(String productName, String type, int num, float price, float buyPrice, String specs, String productId,String vendorName,String orderIdNum,int totalNum,String imgPath) {
        String idNum = "P"+System.currentTimeMillis() ;
        infoBean.setIdNum(idNum);
        service.add(infoBean);

        OrderProductBean productBean =
                new OrderProductBean(infoBean.getOrderIdNum(), productName, type, num, buyPrice, price,infoBean.getOrderInfoId());
        infoBean = new OrderInfoBean();
        productService.add(productBean);

        SupplyInfoBean supplyInfoBean = new SupplyInfoBean(productBean.getOpId(),specs,num);
        iSupplyInfoService.add(supplyInfoBean);

        OrderDto dto = new OrderDto(productName, type, num, price, buyPrice, idNum, specs, productId,orderIdNum,imgPath);
        String json = JSON.toJSONString(dto);
        rabbitTemplate.convertAndSend("directExchangeOrderPiCi", "replenishOrderPiCi.key", json);
        System.out.println("发送成功");

        OrderPiCiDto piCiDto = new OrderPiCiDto(orderIdNum,vendorName,0,totalNum);
        String piCiJson = JSON.toJSONString(piCiDto);
        rabbitTemplate.convertAndSend("directExchangeOrderPiCiZhu", "replenishOrderPiCiZhu.key", piCiJson);
        System.out.println("pici发送成功");

    }

    @GetMapping("updateStatusByIdNum/{idNum}/{status}")
    public void updateStatusByIdNum(@PathVariable("idNum") String idNum, @PathVariable("status") int status) {
        service.updateStatusByOrderInfoIdNum(idNum, status);
    }


}
